package controllers.account;

import static play.data.Form.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.node.ObjectNode;

import controllers.BaseController;

import models.CartItem;
import models.Actor.TYPE;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http.Session;
import play.mvc.Result;
import prefix.util.CipherUtil;
import processor.CartProcessor;
import repository.ActorRepository;
import service.ActorService;
import service.CartService;
import service.impl.ActorServiceImpl;
import service.impl.CartServiceImpl;
import web.entity.User;
import web.util.ResultCode;
import play.mvc.BodyParser;

import views.html.*;
public class Login extends BaseController {
    final static Form<User> regForm = form(User.class);

    public static Result index() {
        return ok(login.render());
    }

    //@BodyParser.Of(BodyParser.Json.class)
    public static Result submit() {
    	ResultCode.ajaxResult ajaxResult=null;
        // DynamicForm requestData = form().bindFromRequest();
       Form<User> filledForm = regForm.bindFromRequest();
        String email = filledForm.field("inputEmail").value();// requestData.get("inputEmail");
        String psw = filledForm.field("inputPassword").value();// requestData.get("inputPassword");
        ActorService actor = ActorRepository.getActorServiceByEmail(email);
        List<String> error=new ArrayList<String>();
        if(actor==null){
        	ajaxResult= ResultCode.ajaxResult.ERROR;        	
        	error.add("用户不存在");
        	ajaxResult.setErrorMessage(error);
        	return ok(ajaxResult.toJson());
        }
        if(!CipherUtil.generatePassword(psw).equals(actor.getDBActor().getCredential())){
        	error.add("账号或密码不正确");
        	ajaxResult=ResultCode.ajaxResult.ERROR;
        	ajaxResult.setErrorMessage(error);
        	return ok(ajaxResult.toJson());
        }
        try {

        	ajaxResult=ResultCode.ajaxResult.SUCCESS;
        } catch (Throwable e) {
        	ajaxResult=ResultCode.ajaxResult.ERROR;
            return ok(ajaxResult.toJson());
        }
        return ok(ajaxResult.toJson());
    }
}
